package com.example.realm;

import com.example.entity.Userinfo;
import com.example.service.UserinfoService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserRealm extends AuthorizingRealm
{
    @Autowired
    private UserinfoService userinfoService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)//授权
    {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken Token) throws AuthenticationException//认证
    {
        UsernamePasswordToken userToken = (UsernamePasswordToken) Token;
        Userinfo userinfo = userinfoService.select(userToken.getUsername());
        ByteSource salt = ByteSource.Util.bytes(userToken.getUsername());
        if (userinfo == null)
        {
            return null;
        }
        return new SimpleAuthenticationInfo(userinfo, userinfo.getPassword(), salt, getName());
    }
}
